package controller

import (
	"fmt"
	"myblogs/app/variable"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type TagController struct {
	*BaseController
}

func (t *TagController) Index(c *gin.Context) {
	tagList := tagModel.GetTagAll(0)
	articleTag := tagModel.GetTagArticleTotal()
	for _, tag := range tagList {
		if len(tag.Cover) > 0 {
			tag.Cover = variable.ImgHost + tag.Cover
		}
		for _, at := range articleTag {
			tagId := at["tagId"]
			if tag.Id == tagId {
				tag.Total = at["total"]
			}
		}
	}

	articleLatest := articleModel.GetArticleLatest()
	articleTotal := articleModel.GetArticleTotal()
	categoryTotal := categoryModel.GetCategoryTotal()
	tags := tagModel.GetTagAll(10)

	data := gin.H{
		"articleLatest": articleLatest,
		"articleTotal":  articleTotal,
		"categoryTotal": categoryTotal,
		"tags":          tags,
		"tagList":       tagList,
		"tagCount":      len(tagList),
		"title":         "标签_全栈工匠",
		"keywords":      "全栈开发，系统架构，后端博客，前端博客，后端框架，前端框架，PHP，Java，C#，Golang，Python，Vue，Html，Javascript，CSS",
		"description":   "一个专注于分享全栈以及系统架构技术和趋势的博客",
		"site":          "zhongxc.cc",
		"pvCount":       t.GetPv(),
		"likeCount":     articleModel.GetLikeCount(),
	}

	// 渲染模板
	c.HTML(http.StatusOK, "tags.tmpl", data)
}

func (t *TagController) GetArticleByTag(c *gin.Context) {
	tag := c.Param("tag")
	tagEscape := t.ReplaceSpecifiedChar(tag)
	keyword := c.DefaultQuery("keyword", "")
	p := c.DefaultQuery("page", "1")

	page, _ := strconv.ParseInt(p, 10, 64)

	pageResult := articleModel.GetArticlePage(page, 10, "", tagEscape, "", "")
	pageResult["keyword"] = ""
	pageResult["tag"] = tag

	pageCount := pageResult["pageCount"]
	pc, err := strconv.ParseInt(fmt.Sprintf("%v", pageCount), 10, 64)
	if err != nil {
		fmt.Printf("%d\n", pc)
	}
	var pageHtml []map[string]string
	for i := 0; i < int(pc); i++ {
		if len(pageHtml) >= 5 {
			break
		}
		pageIndex := fmt.Sprintf("%d", i+1)
		ph := map[string]string{
			"url":       "/search?keyword=" + keyword + "&page=" + pageIndex,
			"pageIndex": pageIndex,
		}
		pageHtml = append(pageHtml, ph)
	}

	pageResult["pageHtml"] = pageHtml

	// 下一分页
	nextPage := page + 1
	if page == pc {
		pageResult["nextPage"] = page
		pageResult["nextPageUrl"] = fmt.Sprintf("/tags/%s?page=%d", keyword, page)
	} else {
		pageResult["nextPage"] = nextPage
		pageResult["nextPageUrl"] = fmt.Sprintf("/tags/%s?page=%d", keyword, nextPage)

	}

	if page > 1 {
		pageResult["prevPage"] = page - 1
		pageResult["prevPageUrl"] = fmt.Sprintf("/tags/%s?page=%d", keyword, page-1)
	} else {
		pageResult["prevPage"] = page
		pageResult["prevPageUrl"] = fmt.Sprintf("/tags/%s?page=%d", keyword, page)
	}

	pageResult["title"] = "全栈工匠-" + tag
	pageResult["keywords"] = "全栈开发，系统架构，后端博客，前端博客，后端框架，前端框架，PHP，Java，C#，Golang，Python，Vue，Html，Javascript，CSS"
	pageResult["description"] = "一个专注于分享全栈以及系统架构技术和趋势的博客"
	pageResult["site"] = "zhongxc.cc"
	pageResult["pvCount"] = t.GetPv()
	pageResult["likeCount"] = articleModel.GetLikeCount()

	// 渲染模板
	c.HTML(http.StatusOK, "search.tmpl", pageResult)
}
